package com.me.bbs.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.bbs.entity.LatestReadAt;
import com.me.bbs.mapper.LatestReadAtMapper;
import com.me.bbs.service.ILatestReadAtService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lm
 * @since 2023-01-24
 */
@Service
public class LatestReadAtServiceImpl extends ServiceImpl<LatestReadAtMapper, LatestReadAt> implements ILatestReadAtService {
    @Resource
    LatestReadAtMapper mapper;

    @Resource
    ILatestReadAtService latestReadAtService;
    @Override
    public Long getUnreadZanCount(Map<String, Object> argsMap) {
        LatestReadAt latestReadAt = latestReadAtService.lambdaQuery()
                .eq(LatestReadAt::getUserId, argsMap.get("uid")).one();
        LocalDateTime latestReadTime = null;
        if(latestReadAt != null)
            latestReadTime = latestReadAt.getReadZanAt();
        argsMap.put("latestTime", latestReadTime);
        return mapper.queryUnreadZanCount(argsMap);
    }

    @Override
    public Page<Map<String, Object>> getUnreadZanMsg(IPage page, Map<String, Object> argsMap) {
        LatestReadAt latestReadAt = latestReadAtService.lambdaQuery()
                .eq(LatestReadAt::getUserId, argsMap.get("uid")).one();
        if(latestReadAt == null) {
            latestReadAt = new LatestReadAt();
            latestReadAt.setUserId((Long) argsMap.get("uid"));
        }
        latestReadAt.setReadZanAt((LocalDateTime) argsMap.get("t"));
        latestReadAtService.saveOrUpdate(latestReadAt);
        return mapper.queryUnreadZanMsg(page, argsMap);
    }
}
